package com.nondo.rdp.auth.listener;

import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.event.PointApplicationEvent;
import com.nondo.rdp.auth.service.UserLoginLogService;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.auth.domain.PointsLog;
import com.nondo.rdp.auth.service.PointsLogService;
import com.nondo.rdp.core.domain.Rules;
import com.nondo.rdp.core.service.RulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 积分监听器
 */
@Component
public class PointApplicationListener implements ApplicationListener<PointApplicationEvent> {
    public final static String TYPE_LOGIN = "send_point_login";//登录添加积分
    public final static String TYPE_REGISTER = "send_point_register";//注册添加积分
    public final static String TYPE_SIGN = "send_point_sign";//签到添加积分
    public final static String TYPE_INVITE_SHARE_VIEW = "invite_share_view";//分享阅读获得积分
    public final static String TYPE_INVITE_REGISTER = "send_point_invite_register";//邀请注册获得积分

    private PointApplicationEvent event;

    @Autowired
    private PointsLogService pointsLogService;

    @Autowired
    private RulesService rulesService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserLoginLogService userLoginLogService;

    public void onApplicationEvent(PointApplicationEvent event) {
        System.out.println("接收到事件：" + event.getClass());
        this.event = event;
        if (event.getType().equals(TYPE_LOGIN)) {
            login_handle();
        } else {
            default_handle(event.getType());
        }
    }

    /**
     * 【特殊】登录增加积分
     */
    public void login_handle() {
        //获取登录次数，一天只能增加一次积分
        int loginNum = this.userLoginLogService.countByUserAndCreateDateAfter(event.getUser(), CommUtil.getTodayZeroTime());
        if (loginNum == 0) {
            //获取登录积分规则
            Long sendPoint = this.getSendPoint("send_point_login");
            if (sendPoint > 0) {
                //增加相应积分
                this.addPoint(sendPoint);
            }
        }
    }

    /**
     * 通用操作，没有特殊处理的，获取后直接操作积分
     */
    public void default_handle(String type) {
        //获取登录积分规则
        Long sendPoint = this.getSendPoint(type);
        if (sendPoint > 0) {
            //增加相应积分
            this.addPoint(sendPoint);
        }
    }

    /**
     * 操作积分
     *
     * @param point
     */
    public void addPoint(Long point) {
        Long lastPoints = event.getUser().getPoints() + point;
        //给用户增加积分
        User user = event.getUser();
        user.setPoints(lastPoints);
        this.userService.save(user);

        //增加积分日志
        PointsLog pointsLog = new PointsLog();
        pointsLog.setPoints(point);
        pointsLog.setUserId(event.getUser().getId());
        pointsLog.setLastPoints(lastPoints);

        this.pointsLogService.save(pointsLog);
    }

    /**
     * 根据规则类型获取对应变更积分
     *
     * @param type
     * @return
     */
    private Long getSendPoint(String type) {
        Rules rules = this.rulesService.findByType(type);
        if (rules == null) {
            return new Long((long) 0);
        }
        return Long.parseLong(rules.getValue());
    }
}
